package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;
import breeze.pixel.weather.apps_util.views.mtoolbar.MToolbar;
import breeze.pixel.weather.service.app_widget.WeatherUtils;

public class BrightnessView extends BaseView implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private ImageView image1;
    private SeekBar seek;
    private MTextView progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brightness);
        init();
        initData();
    }

    @Override
    public void init() {
        MToolbar toolbar=find(R.id.AppToolBar);
        toolbar.setTitle("修改图片亮度");
        toolbar.setNavigation(R.mipmap.icon_back,this);
        image1=find(R.id.activitybrightnessImageView1);
        seek=find(R.id.activitybrightnessSeekBar1);
        progress=find(R.id.activitybrightnessTextView1);
    }

    @Override
    public void initData() {
        initImage();
        seek.setMax(200);
        setProgressText(sets.getBrightness());
        seek.setProgress(sets.getBrightness()+100);
        seek.setOnSeekBarChangeListener(this);
        setImageBitmap(roundedBitmap(sets.getBrightness()));
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View p1) {
        if (p1.getId() == R.id.viewtoolbar_img_negative) {
            finish();
        }
    }

    public void setProgressText(int str) {
        StringBuilder sb=new StringBuilder();
        sb.append("亮度:").append(str);
        this.progress.setText(sb);
    }

    public void setImageBitmap(Bitmap bitmap) {
        image1.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar p1, int p2, boolean p3) {
        setProgressText(p2-100);
    }

    @Override
    public void onStartTrackingTouch(SeekBar p1) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar p1) {
        setImageBitmap(roundedBitmap(p1.getProgress()-100));
    }

    public Bitmap roundedBitmap(int brightness) {
        float radius=50f;
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.img_default_bg);
        Bitmap bit=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bit);
        Paint p=new Paint();
        p.setAntiAlias(true);
        Rect rect=new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
        RectF rectf=new RectF(rect);
        canvas.drawARGB(0,0,0,0);
        canvas.drawRoundRect(rectf,radius,radius,p);
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap,rect,rect,p);

        Bitmap bmp = Bitmap.createBitmap(bit.getWidth(), bit.getHeight(),
                bit.getConfig());
        ColorMatrix cMatrix = new ColorMatrix();
        cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1,
                0, 0, brightness,// 改变亮度
                0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

        Canvas canvas2 = new Canvas(bmp);
        // 在Canvas上绘制一个已经存在的Bitmap。这样，dstBitmap就和srcBitmap一摸一样了
        canvas2.drawBitmap(bit, 0, 0, paint);
        return bmp;
    }

    public void initImage() {
        Bitmap bitmap= AppToolUtil.roundedBitmap(sets.getBrightness(),BitmapFactory.decodeResource(getResources(),R.mipmap.img_default_bg),50f);
        setImageBitmap(bitmap);
    }

    public void save(View v){
        sets.setBrightness(seek.getProgress()-100);
        WeatherUtils.updateView(null);
        toast("保存成功");
        finish();
    }
}